package com.example.testapplication.activity

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.testapplication.R
import com.example.testapplication.databinding.ActivityRecyclerTestBinding
import com.example.testapplication.recyclerUtils.BaseAdapter
import com.example.testapplication.recyclerUtils.BaseItemTouchHelper
import com.example.testapplication.recyclerUtils.BaseViewHolder


class RecyclerTestActivity : AppCompatActivity() {

    private lateinit var mBinding: ActivityRecyclerTestBinding
    private lateinit var adapter: BaseAdapter<String>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_recycler_test)

        val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        mBinding.recycler.layoutManager = layoutManager
        adapter = getAdapter(this)
        mBinding.recycler.adapter = adapter

        val itemTouchHelper = ItemTouchHelper(BaseItemTouchHelper<String>())
        itemTouchHelper.attachToRecyclerView(mBinding.recycler)

        mBinding.day.setOnClickListener {
            for (i in 0 until adapter.getData().size){
                Log.e("data" , "-->${adapter.getData()[i]}")
            }
        }
    }

    private fun getAdapter(context: Context): BaseAdapter<String> {
        val list = mutableListOf<String>()
        for (i in 0 until 20) {
            list.add("-->$i")
        }
        val adapter = object : BaseAdapter<String>(context, R.layout.item_recycler_test, list) {
            override fun onBindViewHolder(holder: BaseViewHolder<String>) {
                holder.itemView.findViewById<TextView>(R.id.text).text = "---->" + holder.item
            }
        }
        return adapter
    }
}